<template>
    <div id="deptadmin">
        <el-button type="primary" style="margin-bottom: 10px;" @click="openUser()" icon="Plus">添加</el-button>
        <div class="tablecon" style="height: 600px;">
            <el-table id="out" border :data="deptList" stripe >
                <el-table-column align="center" type="index" label="序号" width="60" />
                <el-table-column prop="name" label="部门名称" width="250" />
                <el-table-column prop="code" label="部门编码" width="300" />
                <el-table-column label="">
                    <template #default="scope">
                        <el-button size="default" type="success" @click="updateDept(scope.row)" icon="Edit">修改</el-button>
                        <el-button size="default" type="warning" @click="removeDept(scope.row)" icon="Delete">删除</el-button>
                        <!-- <el-button size="small" type="primary" @click="watchUserRole(scope.row)">查看用户角色</el-button> -->
                    </template>
                </el-table-column>
                <!-- <el-table-column prop="" label="" /> -->
            </el-table>
        </div>
        <pagination style="margin-top: 10px;" @sendsize="getsize" @sendpage="getpage" :page="page"></pagination>
        <!-- 用户弹框 -->
        <el-dialog v-model="DeptDlg" title="部门管理" width="500" align-center :before-close="cancle">
            <div>
                <el-form :model="deptForm" label-width="100" class="demo-form-inline" style="margin-top: 10px;">
                    <el-form-item label="部门名称：">
                        <el-input v-model="deptForm.name" placeholder="部门名称" />
                    </el-form-item>
                    <el-form-item label="部门编码：">
                        <el-input v-model="deptForm.code" placeholder="部门编码" />
                    </el-form-item>
                </el-form>
            </div>
            <template #footer>
                <div class="dialog-footer">
                    <el-button type="primary" @click="addUser()"  icon="Select">
                        确定
                    </el-button>
                    <el-button @click="cancle()"  icon="Close">取消</el-button>
                    
                </div>
            </template>
        </el-dialog>
        <!-- 用户角色弹框 -->
        <el-dialog v-model="roleDlg" title="角色管理" width="700" align-center>
            <div>
                <el-table border :data="userroleList" style="width: 100%">
                    <el-table-column type="index" label="序号" width="80" />
                    <el-table-column prop="name" label="角色名称" width="130" />
                    <el-table-column prop="remark" label="备注" />
                    <el-table-column label="操作">
                        <template #default="scope">
                            <el-button type="danger" @click="DelRole(scope.row)" icon="Delete">删除角色</el-button>
                        </template>
                    </el-table-column>
                    <!-- <el-table-column prop="" label="" /> -->
                </el-table>
            </div>
            <template #footer>
                <div class="dialog-footer">
                    <el-button type="success" @click="openRoleDlg()" icon="Plus">添加角色</el-button>

                    <el-button type="primary" @click="roleDlg = false; getDeptList()" icon="Close">
                        关闭
                    </el-button>
                </div>
            </template>
        </el-dialog>
        <!--角色列表弹框 -->
        <el-dialog v-model="roleListDlg" title="角色列表" width="300" align-center :before-close="cancelRoleChoice">
            <div>
                <el-form :model="userroleForm" label-width="auto" style="max-width: 600px">
                    <el-form-item label="选择角色">
                        <el-select v-model="userroleForm.role_code" placeholder="请选择角色">
                            <el-option v-for="item in RoleList" :label="item.name" :value="item.code" />
                        </el-select>
                    </el-form-item>
                </el-form>
            </div>
            <template #footer>
                <div class="dialog-footer">
                    <el-button @click="cancelRoleChoice">取消</el-button>
                    <el-button type="primary" @click="addRole()">
                        确定
                    </el-button>
                </div>
            </template>
        </el-dialog>
    </div>
</template>

<script setup>
import { onMounted, ref, reactive } from "vue";
import { ElMessage, ElMessageBox } from 'element-plus'
import $sys from "../../api/sys";
import pagination from '../../components/pagination.vue';
import * as ElementPlusIconsVue from '@element-plus/icons-vue';

const deptList = ref([]);
const roleDlg = ref(false);
const page = reactive({
    pagenum: 1,
    pagesize: 10,
    total: 0
})
onMounted(() => {
    getDeptList();
    getRoleList();
})
function cancelRoleChoice() {
    roleListDlg.value = false;
    userroleForm.role_code = '';
}
function DelRole(e) {
    ElMessageBox.confirm(
        '确定删除该角色?',
        '警告',
        {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
        }
    ).then(() => {
        console.log(e);
        let obj = {};
        obj.role_code = e.code;
        obj.user_id = userID.value;
        $sys.DelUserRole(obj).then(res => {
            if (res.code == 200) {
                ElMessage({
                    message: '删除成功',
                    type: 'success'
                })
                let obj = {};
                obj.id = userID.value;
                watchUserRole(obj);
            }
        })
    })
}
const roleListDlg = ref(false);
const userroleForm = reactive({});
function addRole() {
    if (userroleForm.role_code == '' || !userroleForm.role_code) {
        ElMessage({
            message: '请选择角色',
            type: 'warning',
        })
    } else {
        userroleForm.user_id = userID.value;
        $sys.addUserRole(userroleForm).then(res => {
            if (res.code == 200) {
                ElMessage({
                    message: '添加成功',
                    type: 'success'
                })
                cancelRoleChoice();
                let obj = {};
                obj.id = userID.value;
                watchUserRole(obj);
            }
        })
    }
}
function openRoleDlg() {
    roleListDlg.value = true;
}
const RoleList = ref([]);
function getRoleList() {
    $sys.getRoleList().then(res => {
        if (res.code == 200) {
            RoleList.value = res.data;
        }
    })
}
const userroleList = ref([]);
const userID = ref('');
function watchUserRole(e) {
    userID.value = e.id;
    $sys.getRoleListByUser(e.id).then(res => {
        if (res.code == 200) {
            userroleList.value = res.data;
            roleDlg.value = true;
        }
    })
}
function addUser() {
    if (checkForm()) {
        $sys.addDept(deptForm).then(res => {
            if (res.code == 200) {
                ElMessage({
                    message: '添加成功',
                    type: 'success'
                });
                getDeptList();
                initdeptForm();
                DeptDlg.value = false;
            }
        })
    }
}
const DeptDlg = ref(false);
const deptForm = reactive({
    username: '',
    password: ''
});
function cancle() {
    initdeptForm();
    getDeptList();
    DeptDlg.value = false;
}
function initdeptForm() {
    deptForm.name = '';
    deptForm.code = '';
    deptForm.parent_code = 'top';
}
function openUser() {
    initdeptForm();
    DeptDlg.value = true;
}
function checkForm() {
    let flag = false;
    if (!deptForm.name || deptForm.name == '') {
        ElMessage({
            message: '部门名称不能为空!',
            type: 'warning'
        });
        flag = false;
    } else if (!deptForm.code || deptForm.code == '') {
        ElMessage({
            message: '部门编码不能为空!',
            type: 'warning'
        });
        flag = false;
    } else {
        flag = true;
    }
    return flag;
}
function updateDept(e) {
    $sys.getDeptByID(e.id).then(res => {
        if (res.code == 200) {
            deptForm.name = res.data.name;
            deptForm.code = res.data.code;
            DeptDlg.value = true;
        }
    })
}
function removeDept(e) {
    ElMessageBox.confirm(
        '是否确定？',
        '警告',
        {
            cancelButtonText: '取消',
            confirmButtonText: '确定',            
            type: 'warning',
        }
    ).then(() => {
        $sys.delDept(e.id).then(res => {
            if (res.code == 200) {
                ElMessage({
                    message: '操作成功',
                    type: 'success'
                })
                getDeptList();
            }
        })
    }).catch(() => {

    })

}
function getsize(e) {
    page.pagesize = Number(e);
    getDeptList();
}
function getpage(e) {
    page.pagenum = Number(e);
    getDeptList();
}
function getDeptList() {
    $sys.getDeptList(page).then(res => {
        if (res.code == 200) {
            page.total = res.data.count;
            deptList.value = res.data.result;
        }
    })
}
</script>

<style scoped>
:deep(*) {
    font-size: medium;
}

.el-form-item__label {
    color: deepskyblue;
}

#deptadmin {
    background-color: #2F4052;
    width: 100%;
    padding: 0 10px;
    height: 780px;
    padding-top: 10px;
}

.tablecon {
    overflow-y: auto;
}

.tablecon::-webkit-scrollbar {
    width: 5px;
}

.tablecon::-webkit-scrollbar-thumb {
    background-color: #ccc;
    /* 设置滚动条的颜色 */
    border-radius: 6px;
    /* 设置滚动条的圆角 */
}

.tablecon::-webkit-scrollbar-track {
    background-color: #888;
    border-radius: 6px;
    /* 设置滚动条的轨道颜色 */
}

:deep(.el-dialog) {
    background-color: #2F4052 !important;
}

:deep(.el-dialog__title),
:deep(.el-dialog__body) {
    color: white !important;
}
</style>

<style lang='less'>
#deptadmin {
    .el-table--striped .el-table__body tr.el-table__row--striped td.el-table__cell {
        background: none;
    }

    .el-table__inner-wrapper {
        &::before {
            display: none;
        }
    }

    // 头部样式
    .el-table__header-wrapper th {
        background-color: #2F4052;
        color: #fff;
    }

    // 内容行样色
    .el-table__row {
        background-color: #2F4052;
        color: #B2C9EE;
    }

    // 内容行边框
    .el-table__cell {
        border: 1 !important;
        border-color: #222e3b;
    }

    .el-table--border .el-table__inner-wrapper:after,
    .el-table--border:after,
    .el-table__border-left-patch {
        background-color: #222e3b;
    }

    //自定义斑马线和鼠标移入样式
    .el-table--enable-row-hover .el-table__body tr:hover:nth-child(even)>td {
        background-color: #263343 !important;
    }

    .el-table--enable-row-hover .el-table__body tr:hover:nth-child(odd)>td {
        background-color: #2F4052 !important;
    }

    .el-table__body tr:nth-child(even) {
        background-color: #263343;
        /* 偶数行（斑马线）的默认背景色 */
    }

    .el-table__empty-block {
        background-color: #233741;
    }

    .el-form-item__label {
        color: deepskyblue;
    }
}
</style>